<?php
@define('IN_XLR', TRUE);

ob_start();//使用缓冲区来捕获非法数据数据

//程序目录
define('S_ROOT', dirname(__FILE__).DIRECTORY_SEPARATOR);

include_once(S_ROOT.'config.php');

include_once S_ROOT."class/db/ez_sql_core.php";
include_once S_ROOT."class/db/ez_sql_mysql.php";

include_once(S_ROOT.'function_common.php');
 
//时间
$mtime = explode(' ', microtime());
$_SGLOBAL['timestamp'] = $mtime[1];
$_SGLOBAL['timestamp_super'] = $mtime[1] + $mtime[0];




//获取header变量
/*
$headers = array(); 
$headers['returncode'] = "0";
foreach ($_SERVER as $key => $value) { 
    if ('HTTP_' == substr($key, 0, 5)) { 
        $headers[strtoupper(str_replace('_', '-', substr($key, 5)))] = $value; 
    }
}
$apiversion = $_SGLOBAL['apiversion'] = $headers[strtoupper(APIHEADER_APIVERSION)];

$appid = $_SGLOBAL['appid'] = $headers[strtoupper(APIHEADER_APPID)];
$requestid = $_SGLOBAL['requestid'] = $headers[strtoupper(APIHEADER_REQUESTID)];
$request_sign = $_SGLOBAL['request_sign'] = $headers[strtoupper(APIHEADER_SIGN)];

$request_body = empty($_GET['body'])?$_POST['body']:$_GET['body'];
$headers = NULL;

api_log("[INP]\t$apiversion\t$appid\t$requestid\t$request_sign\t$request_body");


//检查请求参数
if(empty($appid) || empty($requestid) || empty($request_sign)){
	//请求包头参数错误
	$_ApiOut["returncode"] = "1003";
	goto FINISH;
}else if(empty($request_body)){
	//请求包体参数错误
	$_ApiOut["returncode"] = "1004";
	goto FINISH;
}

//检查签名
$request_sign = strtolower(trim($request_sign));
$check_sign = strtolower(md5($_CONFIG['appkey_map'][$appid].trim($requestid).trim($request_body)));
if($request_sign <> $check_sign){
	//请求包签名错误
	$_ApiOut["returncode"] = "1005";
	goto FINISH;
}
*/

//获取包体
//$_ApiReq = json_decode($request_body, true);
$_ApiReq = empty($_POST)?$_GET:$_POST;


//检查Action参数
$action = $_ApiReq['action'];
if(empty($action) || $action==""){
	//请求包Action参数错误
	$_ApiOut["returncode"] = "1006";
	goto FINISH;
}

//source目录
if(empty($apiversion)){
	$apiversion = '1.0';
}
$source_path = 'source'."_".$apiversion;
if(!is_dir(S_ROOT.$source_path)){
	$_ApiOut["returncode"] = "1011";
	$_ApiOut["apiversion"] = $_SGLOBAL['apiversion'];
	goto FINISH;
}

//检查Action文件
if(!file_exists(S_ROOT.$source_path."/api_{$action}.php")){
	//请求的Action源码不存在
	$_ApiOut["returncode"] = "1007";
	$_ApiOut["apiversion"] = $_SGLOBAL['apiversion'];
	goto FINISH;
}

//包含处理脚本代码
include_once(S_ROOT.$source_path."/api_{$action}.php");

//检查Action文件
if(!function_exists("api_{$action}")){
	//请求的action函数名不正确
	$_ApiOut["returncode"] = "1008";
	$_ApiOut["apiversion"] = $_SGLOBAL['apiversion'];
	goto FINISH;
}

//执行Action动作，处理逻辑
$_CallFunction = 'api_'.$action;
$_ApiOut = $_CallFunction($_ApiReq);

//组装回应包体
FINISH:
$response_body = json_encode($_ApiOut);
$serverid = $_CONFIG['serverid'];
$responseid = $requestid;
$response_sign = md5($_CONFIG['serverkey'].$responseid.$response_body);

ob_end_clean();//清除缓冲区中非法数据，并关闭缓冲区

//往客户端数据
header(APIHEADER_APIVERSION.":   $apiversion ",   true); 
header(APIHEADER_SERVERID.":   $serverid ",   true); 
header(APIHEADER_SIGN.":   $response_sign ",   true); 
header(APIHEADER_RESPONSEID.":   $responseid ",   true); 
echo $response_body;
api_log("[OUT]\t$serverid\t$responseid\t$response_sign\t$response_body");

//关闭数据库连接
master_db_close();
slave_db_close();
?>